| Conditions | 1 |
| Paths | 2 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function(state, visibility, upgradeService, data) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.upgradeService = upgradeService; |
||
| 21 | let sortFunc = upgradeService.sortFunctions(data.upgrades); |
||
| 22 | |||
| 23 | // tries to buy all the upgrades it can, starting from the cheapest |
||
| 24 | ct.buyAll = function (slot, player) { |
||
| 25 | let currency = data.elements[slot.element].main; |
||
| 26 | let cheapest; |
||
| 27 | let cheapestPrice; |
||
| 28 | do{ |
||
| 29 | cheapest = null; |
||
| 30 | cheapestPrice = Infinity; |
||
|
|
|||
| 31 | for(let up of ct.visibleUpgrades(slot, player)){ |
||
| 32 | let price = data.upgrades[up].price; |
||
| 33 | if(!slot.upgrades[up] && |
||
| 34 | price <= player.resources[currency].number){ |
||
| 35 | if(price < cheapestPrice){ |
||
| 36 | cheapest = up; |
||
| 37 | cheapestPrice = price; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | if(cheapest){ |
||
| 42 | upgradeService.buyUpgrade(player, |
||
| 43 | slot.upgrades, |
||
| 44 | data.upgrades[cheapest], |
||
| 45 | cheapest, |
||
| 46 | cheapestPrice, |
||
| 47 | currency); |
||
| 48 | } |
||
| 49 | }while(cheapest); |
||
| 50 | }; |
||
| 51 | |||
| 52 | ct.buyUpgrade = function (name, slot, player) { |
||
| 53 | let price = data.upgrades[name].price; |
||
| 54 | let currency = data.elements[slot.element].main; |
||
| 55 | upgradeService.buyUpgrade(player, |
||
| 56 | slot.upgrades, |
||
| 57 | data.upgrades[name], |
||
| 58 | name, |
||
| 59 | price, |
||
| 60 | currency); |
||
| 61 | }; |
||
| 62 | |||
| 63 | ct.visibleUpgrades = function(slot, player) { |
||
| 64 | return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[player.options.sortIndex], player); |
||
| 65 | }; |
||
| 66 | |||
| 67 | function isBasicUpgradeVisible(name, slot, player) { |
||
| 68 | let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name], player); |
||
| 69 | return isVisible && (!player.options.hideBought || !slot.upgrades[name]); |
||
| 70 | } |
||
| 71 | |||
| 72 | ct.visibleGlobalUpgrades = function() { |
||
| 73 | return visibility.visible(data.global_upgrades, upgradeService.filterByTag('global')); |
||
| 74 | }; |
||
| 75 | } |
||
| 76 | ]); |
||
| 77 |